using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class FaceFeatureSubMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.ffs_Menu, new Point(0, -107));
    private static List<dynamic> scaleList = new List<object>(new string[1] { string.Empty });
    private static UIMenuListItem[] FaceFeatureItem = new UIMenuListItem[Skin.FACE_FEATURE.Length];

    public FaceFeatureSubMenu()
    {
        Main.pool.Add(menu);
        menu.OnListChange += OnListChange;
        menu.OnItemSelect += OnItemSelect;
        menu.OnMenuClose += OnMenuClose;
    }

    public static void InitFaceFeatureSubMenu()
    {
        menu.Clear();
        scaleList = new List<object>();
        for (double num = -1.0; num <= 1.0; num += 0.1) scaleList.Add(Math.Round(num, 1));
        if (DressingScript.Dressing == Main.Member.P2)
        {
            for (int i = 0; i < Skin.FACE_FEATURE.Length; i++)
            {
                FaceFeatureItem[i] = new UIMenuListItem(Skin.FACE_FEATURE[i], scaleList,
                    scaleList.FindIndex(name => (float)name == Main.P2.skin.Face_Feature[i].featureValue));
                menu.AddItem(FaceFeatureItem[i]);
            }
        }
        else
        {
            for (int i = 0; i < Skin.FACE_FEATURE.Length; i++)
            {
                FaceFeatureItem[i] = new UIMenuListItem(Skin.FACE_FEATURE[i], scaleList,
                    scaleList.FindIndex(name => (float)name == Main.P1.skin.Face_Feature[i].featureValue));
                menu.AddItem(FaceFeatureItem[i]);
            }
        }
        menu.SetBannerType(new UIResRectangle());
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
        menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
        menu.RefreshIndex();
        menu.Visible = true;
    }

    private static void UpdateFaceFeatureValue()
    {
        if (DressingScript.Dressing == Main.Member.P2)
        {
            for (int i = 0; i < Skin.FACE_FEATURE.Length; i++)
            {
                Main.P2.skin.Face_Feature[i].featureValue = (float)scaleList[FaceFeatureItem[i].Index];
            }
            return;
        }
        for (int j = 0; j < Skin.FACE_FEATURE.Length; j++)
        {
            Main.P1.skin.Face_Feature[j].featureValue = (float)scaleList[FaceFeatureItem[j].Index];
            Main.Clone.skin.Face_Feature[j].featureValue = (float)scaleList[FaceFeatureItem[j].Index];
        }
    }

    private static void OnListChange(UIMenu sender, UIMenuListItem list, int index)
    {
        int idx = Array.FindIndex(Skin.FACE_FEATURE, name => name == list.Text);
        if (DressingScript.Dressing == Main.Member.P2)
        {
            SkinHelper.SetPedFaceFeature(Main.P2.Character, idx, (float)scaleList[FaceFeatureItem[idx].Index]);
            Main.P2.skin.Face_Feature[idx].action = true;
            return;
        }
        SkinHelper.SetPedFaceFeature(Main.P1.Character, idx, (float)scaleList[FaceFeatureItem[idx].Index]);
        SkinHelper.SetPedFaceFeature(Main.Clone.Character, idx, (float)scaleList[FaceFeatureItem[idx].Index]);
        Main.P1.skin.Face_Feature[idx].action = true;
        Main.Clone.skin.Face_Feature[idx].action = true;
    }

    private static void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
    {
        switch (index)
        {
            case 20:
                {
                    Random random = new Random(Guid.NewGuid().GetHashCode());
                    for (int l = 0; l < Skin.FACE_FEATURE.Length; l++) FaceFeatureItem[l].Index = random.Next(0, scaleList.Count);
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        for (int m = 0; m < Skin.FACE_FEATURE.Length; m++)
                        {
                            SkinHelper.SetPedFaceFeature(Main.P2.Character, m, (float)scaleList[FaceFeatureItem[m].Index]);
                            Main.P2.skin.Face_Feature[m].action = true;
                        }
                        break;
                    }
                    for (int n = 0; n < Skin.FACE_FEATURE.Length; n++)
                    {
                        SkinHelper.SetPedFaceFeature(Main.P1.Character, n, (float)scaleList[FaceFeatureItem[n].Index]);
                        SkinHelper.SetPedFaceFeature(Main.Clone.Character, n, (float)scaleList[FaceFeatureItem[n].Index]);
                        Main.P1.skin.Face_Feature[n].action = true;
                        Main.Clone.skin.Face_Feature[n].action = true;
                    }
                    break;
                }
            case 21:
                {
                    for (int i = 0; i < Skin.FACE_FEATURE.Length; i++) FaceFeatureItem[i].Index = 10;
                    if (DressingScript.Dressing == Main.Member.P2)
                    {
                        for (int j = 0; j < Skin.FACE_FEATURE.Length; j++)
                        {
                            SkinHelper.SetPedFaceFeature(Main.P2.Character, j, 0f);
                            Main.P2.skin.Face_Feature[j].action = false;
                        }
                        break;
                    }
                    for (int k = 0; k < Skin.FACE_FEATURE.Length; k++)
                    {
                        SkinHelper.SetPedFaceFeature(Main.P1.Character, k, 0f);
                        SkinHelper.SetPedFaceFeature(Main.Clone.Character, k, 0f);
                        Main.P1.skin.Face_Feature[k].action = false;
                        Main.Clone.skin.Face_Feature[k].action = false;
                    }
                    break;
                }
            case 22:
                DressingScript.Distance();
                break;
            case 23:
                DressingScript.TurnLeft();
                break;
            case 24:
                DressingScript.TurnRight();
                break;
            case 25:
                DressingScript.AngleUp();
                break;
            case 26:
                DressingScript.AngleDown();
                break;
        }
    }

    private static void OnMenuClose(UIMenu sender)
    {
        // 关闭时不做状态写回；变更已在 OnListChange/OnItemSelect 生效
        menu.Visible = false;
        // 不刷新，避免退格重入
    }
}


